package com.example.wlzz.service.serviceImpl;

import com.example.wlzz.dto.ApplyFriendDTO;
import com.example.wlzz.dto.FriendsDTO;
import com.example.wlzz.dto.OpenUserInfoDTO;
import com.example.wlzz.entity.UserInfo;
import com.example.wlzz.entity.mongoEntity.UserWithFriends;
import com.example.wlzz.enums.FriendEnum;
import com.example.wlzz.enums.ResultEnum;
import com.example.wlzz.repository.mongoRepository.UserWithFriendsRepository;
import com.example.wlzz.service.UserInfoService;
import com.example.wlzz.service.UserWithFriendsService;
import com.example.wlzz.utils.ResultVOUtil;
import com.example.wlzz.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class UserWithFriendsServerImpl implements UserWithFriendsService {
    @Autowired
    private UserWithFriendsRepository userWithFriendsRepository;
    @Autowired
    private UserInfoService userInfoService;
    @Override
    public UserWithFriends save(UserWithFriends userWithFriends) {
        return userWithFriendsRepository.save(userWithFriends);
    }
    @Override
    public ResultVO getApplyedList(int userid) {  //获取申请好友列表
        List<UserWithFriends> userWithFriendList =  userWithFriendsRepository.findByToUseridOrderByCreateTimeDesc(userid);
        List<ApplyFriendDTO> applyFriendDTOList = new ArrayList<>();
        for (UserWithFriends userWithFriends : userWithFriendList) {
            ApplyFriendDTO applyFriendDTO = new ApplyFriendDTO();
            ResultVO resultVO;
            if(userWithFriends.getUserid().equals(userid)){   //说明那时候自己就是申请添加好友的那个
                 resultVO =  userInfoService.useridGetForInfo(userWithFriends.getToUserid());
            }else{
                 resultVO =  userInfoService.useridGetForInfo(userWithFriends.getUserid());
            }
            applyFriendDTO.setUserWithFriends(userWithFriends);
            applyFriendDTO.setUser((OpenUserInfoDTO)resultVO.getData());
            applyFriendDTOList.add(applyFriendDTO);
        }
        return ResultVOUtil.success(applyFriendDTOList);
    }
    @Override
    public ResultVO applyForFriends(int userid, int toUserid) {  //申请添加好友
        UserInfo userInfo = userInfoService.findByUserId(toUserid);
        if(userInfo==null){
            return ResultVOUtil.error(ResultEnum.USER_NO_EXIST.getCode(),ResultEnum.USER_NO_EXIST.getMessage());
        }
        if(userid==toUserid){
            return ResultVOUtil.error(ResultEnum.FRIEND_FOR_SELF.getCode(),ResultEnum.FRIEND_FOR_SELF.getMessage());
        }
        List<UserWithFriends> userWithFriendList1 =
              userWithFriendsRepository.findByUseridAndToUseridAndFlag(userid,toUserid, FriendEnum.NO_DEAL.getCode());//查询是否已有相同此操作
        List<UserWithFriends> userWithFriendList2 =
                userWithFriendsRepository.findByUseridAndToUseridAndFlag(userid,toUserid, FriendEnum.SUCCESS.getCode());//查询是否已有相同此操作
        if(userWithFriendList1.size()>0){
            return ResultVOUtil.error(ResultEnum.APPLY_FRIENDED.getCode(),ResultEnum.APPLY_FRIENDED.getMessage());
        }
        if(userWithFriendList2.size()>0){
            return ResultVOUtil.error(ResultEnum.FRIENDED.getCode(),ResultEnum.FRIENDED.getMessage());
        }

        List<UserWithFriends> userWithFriendList3 =
                userWithFriendsRepository.findByUseridAndToUseridAndFlag(toUserid,userid, FriendEnum.NO_DEAL.getCode());//查询是否已有相同此操作
        List<UserWithFriends> userWithFriendList4 =
                userWithFriendsRepository.findByUseridAndToUseridAndFlag(toUserid,userid, FriendEnum.SUCCESS.getCode());//查询是否已有相同此操作
        if(userWithFriendList3.size()>0){
            return ResultVOUtil.error(ResultEnum.APPLY_FRIENDED.getCode(),ResultEnum.APPLY_FRIENDED.getMessage());
        }
        if(userWithFriendList4.size()>0){
            return ResultVOUtil.error(ResultEnum.FRIENDED.getCode(),ResultEnum.FRIENDED.getMessage());
        }
        UserWithFriends userWithFriend = new UserWithFriends();
        //userWithFriend.set_id(KeyUtil.genUniqueKey());  //让mongoDB自己生成速度较快
        userWithFriend.setFlag(FriendEnum.NO_DEAL.getCode());
        userWithFriend.setUserid(userid);
        userWithFriend.setToUserid(toUserid);
        userWithFriend.setCreateTime(new Date());
        return ResultVOUtil.success(userWithFriendsRepository.save(userWithFriend));
    }

    @Override
    public ResultVO agreeApply(String _id, int toUserid) {  //同意好友自己就是那个toUserid
        UserWithFriends userWithFriends =  userWithFriendsRepository.findBy_id(_id);
        if(userWithFriends==null){   //申请列表不存在
            return ResultVOUtil.error(ResultEnum.NO_FRIEND_APPLY.getCode(),ResultEnum.NO_FRIEND_APPLY.getMessage());
        }
        if(FriendEnum.NO_DEAL.getCode()!=userWithFriends.getFlag()){  //判断申请的状态
            return ResultVOUtil.error(ResultEnum.NO_FRIEND_APPLY.getCode(),ResultEnum.NO_FRIEND_APPLY.getMessage());
        }
        if(!userWithFriends.getToUserid().equals(toUserid)){  //判断是否属于自己的申请
            return ResultVOUtil.error(ResultEnum.IIIEGAL_ACTION.getCode(),ResultEnum.IIIEGAL_ACTION.getMessage());
        }
        userWithFriends.setFlag(FriendEnum.SUCCESS.getCode());//接受添加好友
        userWithFriendsRepository.save(userWithFriends);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO rejectApply(String _id, int toUserid) {
        UserWithFriends userWithFriends =  userWithFriendsRepository.findBy_id(_id);
        if(userWithFriends==null){
            return ResultVOUtil.error(ResultEnum.NO_FRIEND_APPLY.getCode(),ResultEnum.NO_FRIEND_APPLY.getMessage());
        }
        if(FriendEnum.NO_DEAL.getCode()!=userWithFriends.getFlag()){  //判断申请的状态
            return ResultVOUtil.error(ResultEnum.NO_FRIEND_APPLY.getCode(),ResultEnum.NO_FRIEND_APPLY.getMessage());
        }
        if(!userWithFriends.getToUserid().equals(toUserid)){  //判断是否属于自己的申请
            return ResultVOUtil.error(ResultEnum.IIIEGAL_ACTION.getCode(),ResultEnum.IIIEGAL_ACTION.getMessage());
        }
        userWithFriends.setFlag(FriendEnum.REJECT.getCode());//接受添加好友
        userWithFriendsRepository.save(userWithFriends);
        return ResultVOUtil.success();
    }

    @Override
    public ResultVO getFriends(int userid) {
        List<UserWithFriends> userWithFriend1 = userWithFriendsRepository.findByUseridAndFlag(userid,FriendEnum.SUCCESS.getCode());
        List<UserWithFriends> userWithFriend2 = userWithFriendsRepository.findByToUseridAndFlag(userid,FriendEnum.SUCCESS.getCode());
        for (UserWithFriends userWithFriends : userWithFriend2) {
            userWithFriend1.add(userWithFriends);
        }
//        userWithFriend1.addAll();  //源码写如果Collection为空会抛空指针
        List<FriendsDTO> friendsDTOList = new ArrayList<>();
        for (UserWithFriends userWithFriends : userWithFriend1) {
            UserInfo userInfo;
            if(userWithFriends.getUserid().equals(userid)) {
                userInfo = userInfoService.findByUserId(userWithFriends.getToUserid());
            }else {
                userInfo = userInfoService.findByUserId(userWithFriends.getUserid());
            }
                FriendsDTO friendsDTO = new FriendsDTO();
                friendsDTO.setImgUrl(userInfo.getImgUrl());
                friendsDTO.setNickname(userInfo.getNickName());
                friendsDTO.setUserid(userInfo.getUserId());
                friendsDTOList.add(friendsDTO);
            }
        return ResultVOUtil.success(friendsDTOList);
    }
}
